//
//  ResumeMakeViewController.m
//  eshow
//
//  Created by Hisoka on 15/11/18.
//  Copyright © 2015年 Hisoka. All rights reserved.
//

#import "ResumeMakeViewController.h"
#import "WebViewJavascriptBridge.h"
#import "ResumePreViewController.h"

static NSString * const H5_URL = @"http://h5.csag.com.cn/Resume/preMake/tid/%@/ssid/%@.html";

@interface ResumeMakeViewController ()<UIWebViewDelegate>

@property (nonatomic, strong) UIWebView *webView;
@property (nonatomic, strong) WebViewJavascriptBridge *bridge;
@property (nonatomic) BOOL saveDraft;

@end

@implementation ResumeMakeViewController

- (void) viewWillDisappear:(BOOL)animated {
    [SVProgressHUD dismiss];
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self customizeBackItem];
    __weak typeof (self) weakSelf = self;
    [self customizeBackItemWithCallBack:^{
        [MobClick event:[NSString stringWithFormat:@"Click_MakeResume_backBTN_%@", self.tid]];

        [_bridge send:@"saveDraft" responseCallback:^(id responseData) {
//            NSLog(@"draft is %@", responseData);
            [MobClick event:[NSString stringWithFormat:@"Click_MakeResume_draftBTN_%@", self.tid]];

            [weakSelf.navigationController popViewControllerAnimated:YES];
        }];

        [MobClick event:[NSString stringWithFormat:@"Click_MakeResume_draftCancelBTN_%@", self.tid]];

    }];
    self.navigationController.interactivePopGestureRecognizer.delegate = nil;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self setStyle];
}

- (void) setStyle {
    self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"生成简历"
                                                                              style:UIBarButtonItemStyleDone
                                                                             target:self
                                                                             action:@selector(reqMakeResume)];
    self.navigationItem.rightBarButtonItem.tintColor = ORANGE_COLOR;
    
    self.webView = [[UIWebView alloc] initForAutoLayout];
    [self.view addSubview:_webView];
    
    [_webView autoPinToTopLayoutGuideOfViewController:self withInset:0];
    [_webView autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:self.view];
    [_webView autoPinEdge:ALEdgeRight toEdge:ALEdgeRight ofView:self.view];
    [_webView autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:self.view];
    _webView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    
    _webView.delegate = self;
    
//    NSString *reqUrl = [NSString stringWithFormat:H5_URL, _tid, [[TMCache sharedCache] objectForKey:@"SSID"]];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:_resumeUrl]];
    
    [_webView loadRequest:request];
    
    self.bridge = [WebViewJavascriptBridge bridgeForWebView:_webView handler:^(id data, WVJBResponseCallback responseCallback) {
        responseCallback(@"objc => Right back atcha");
    }];
    
    [SVProgressHUD show];
    [self performSelector:@selector(dismissSVHUD) withObject:nil afterDelay:1.5f];
    _saveDraft = YES;
}

- (void) dismissSVHUD {
    [SVProgressHUD dismiss];
}

- (void) reqMakeResume {
    [MobClick event:[NSString stringWithFormat:@"Click_MakeResume_addBTN_%@", self.tid]];
    
    [_bridge send:@"subForm" responseCallback:^(id responseData) {
        
        if ([responseData count] <= 0) return;
        
        if ([[responseData objectForKey:@"status"] isEqualToString:@"0"]) {
            ResumePreViewController *resumePreVC = [[ResumePreViewController alloc] init];
            resumePreVC.preViewUrl = [[responseData  objectForKey:@"data"] objectForKey:@"url"];
            resumePreVC.shareContent = [[responseData objectForKey:@"data"] objectForKey:@"text"];
            resumePreVC.shareTitle = [[responseData objectForKey:@"data"] objectForKey:@"share_name"];
            resumePreVC.imageUrl = [[responseData objectForKey:@"data"] objectForKey:@"share_thumb"];
            resumePreVC.tid = self.tid;
//            resumePreVC.rid = [[responseData objectForKey:@"data"] objectForKey:@"rid"];
            resumePreVC.isPopRoot = YES;
            resumePreVC.hasShowMy = YES;
            [self.navigationController pushViewController:resumePreVC animated:YES];
        }
    }];
}

- (BOOL) reqSaveDraft {
    [MobClick event:[NSString stringWithFormat:@"Click_MakeResume_draftBTN_%@", self.tid]];
    
    [_bridge send:@"saveDraft" responseCallback:^(id responseData) {
                
        if ([responseData isEqualToString:@"no"]) {
            _saveDraft = YES;
        }else{
            if ([[responseData objectForKey:@"status"] isEqualToString:@"0"]) {
                _saveDraft = YES;
            }else{
                _saveDraft = NO;
            }
        }
    }];
    
    return _saveDraft;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
